package com.niannian.activity;

import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.MyTextWatcher;
import com.niannian.util.VAR;
import com.niannian.BaseActivity;
import com.niannian.R;

/**
 * 
 * 编辑资料
 * 
 * @author shengl
 * 
 */
public class EditInfoActivity extends BaseActivity {
	private EditText et_content, edt_year, edt_month, edt_day;
	LinearLayout ll_edt_birth;
	private Button bt_save;
	int type = 0;// 用来区分是要修改的是那个一个数据
	String currentData = "";// 当前数据

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_info);
		initView();
		setLister();
		initTopView();
		initData();
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myfinish();
			}
		});
	}

	private void initView() {
		et_content = (EditText) findViewById(R.id.et_content);
		bt_save = (Button) findViewById(R.id.bt_save);
		// et_content.addTextChangedListener(textWatcher);
		ll_edt_birth = (LinearLayout) findViewById(R.id.ll_edt_birth);
		edt_year = (EditText) findViewById(R.id.edt_year);
		edt_month = (EditText) findViewById(R.id.edt_month);
		edt_day = (EditText) findViewById(R.id.edt_day);

	}

	private void initData() {
		Bundle b = getIntent().getExtras();
		type = b.getInt("type");
		currentData = b.getString("currentData");

		switch (type) {
		case VAR.EDIT_NICKNAME:
			top_title.setText("修改昵称");
			et_content.setVisibility(View.VISIBLE);
			ll_edt_birth.setVisibility(View.GONE);
			et_content.setText(currentData);
			break;
		case VAR.EDIT_INNERNAME:
			top_title.setText("修改称呼");
			et_content.setVisibility(View.VISIBLE);
			ll_edt_birth.setVisibility(View.GONE);
			et_content.setText(currentData);
			break;
		case VAR.EDIT_BIRTH:
			top_title.setText("修改生日");
			et_content.setVisibility(View.GONE);
			ll_edt_birth.setVisibility(View.VISIBLE);
			try {
				if (!Common.empty(currentData)) {
					String birthDate[] = currentData.split("-");
					edt_year.setText(birthDate[0]);
					edt_month.setText(birthDate[1]);
					edt_day.setText(birthDate[2]);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			break;
		default:
			break;
		}
		et_content.setSelection(currentData.length());
	}

	private void setLister() {

		bt_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String data = et_content.getText().toString().trim();

				if (type == VAR.EDIT_BIRTH) {
					String year = edt_year.getText().toString().trim();

					if (year.length() != 4) {
						Common.tip(EditInfoActivity.this, "请输入正确的年");
						return;
					}
					int month = 0;
					int day = 0;
					String months = edt_month.getText().toString().trim();
					if (months.length() < 1) {
						Common.tip(EditInfoActivity.this, "请输入正确的月份");
						return;
					} else {
						month = Integer.parseInt(months);
					}
					String days = edt_day.getText().toString().trim();
					if (days.length() < 1) {
						Common.tip(EditInfoActivity.this, "请输入正确的日");
						return;
					} else {
						day = Integer.parseInt(days);
					}
					if (month > 12) {
						Common.tip(EditInfoActivity.this, "请输入正确的月份");
						return;
					}
					if (day > 32) {
						Common.tip(EditInfoActivity.this, "请输入正确的日");
						return;
					}
					data = year + "-" + month + "-" + day;
				} else {
					data = et_content.getText().toString().trim();
					int checkName = Common.checkName(data);
					switch (checkName) {
					case -1:
						Common.tip(EditInfoActivity.this, "名字只能是汉字、数字、字母组合");
						return;
					case -2:
						Common.tip(EditInfoActivity.this, "姓名长度不符合要求");
						return;
					}

				}

				if (!Common.empty(data) && !currentData.equals(data)) {
					Intent i = new Intent();
					i.putExtra("rdata", data);
					setResult(RESULT_OK, i);
					// new SubmitFeedback().init(null).execute();
					myfinish();
				} else {
					myfinish();
				}
			}
		});
	}

	private MyTextWatcher textWatcher = new MyTextWatcher() {

		@Override
		public void onFocusChange(View view, boolean hasFocus) {
		};

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			if (!Common.empty(et_content.getText().toString().trim())) {
				//bt_save.setEnabled(true);
			} else {
				//bt_save.setEnabled(false);
			}
		}
	};

	protected class SubmitData extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			Common.tip(EditInfoActivity.this, "上传资料失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				Common.tip(EditInfoActivity.this, "上传资料成功");
				myfinish();
			} else {
				Common.tip(EditInfoActivity.this, ErrorCode.GetErrorMsg(retCode));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return "";
		}
	}
}
